<?php
/**
 * The Template for displaying all single posts.
 *
 */
 
// if not logged in, redirect to login page
ns_auth_redirect_login();

//Get user info
global $current_user;
get_currentuserinfo();
 
if ( !empty($_POST['action']) && $_POST['action'] == 'update_profile' ) 
{
	check_admin_referer( 'update_profile_' . $user_ID );
	
	if ( defined('ABSPATH') ) {
		require_once(ABSPATH . 'wp-admin/includes/user.php');
	} else {
		require_once('../wp-admin/includes/user.php');
	}
		
	$errors = edit_user( $user_ID );
		
	if ( !is_wp_error( $errors ) )  :
		// pdate meta tags
		$user_meta_fields = array ( 'first_name', 'last_name', 'account_type', 'display_name', 'description', 'vd_adress', 'vd_cp', 'vd_ville', 'vd_department', 'vd_region', 'vd_country', 'vd_tel', 'vd_gsm', 'vd_fax', 'url' );
		foreach($user_meta_fields as $user_meta_field){
			update_user_meta($user_ID, $user_meta_field, sanitize_text_field( $_POST[$user_meta_field]) );
		}
		
		// Redirect
		wp_redirect( './?updated=true' );
	endif;
}

get_header(); ?>

<div id="wrapper_main" class="grid_24">

	<div id="page_title" class="grid_24"><h1><?php _e( 'My account', ns_text_domaine ); ?></h1></div>
	
	<div id="single" class="grid_17 suffix_1 alpha">
		
			<div class="row grid_17">
				<?php if ( isset( $errors ) && is_wp_error( $errors ) ) : ?>
				<div class="error"><p><?php echo implode( "</p>\n<p>", $errors->get_error_messages() ); ?></p></div>
				<?php endif; ?>
				
				<?php if ( isset($_GET['updated']) ) : ?>
				<div class="success"><strong><?php _e( 'Your profile has been successfully updated.', ns_text_domaine ); ?></strong><br /></div>
				<br />
				
				<?php  else : ?>
				<p><?php _e( 'Edit your profile data or change your password.', ns_text_domaine ); ?></p>
				<p>&nbsp;</p>
				<?php  endif; ?>
				
			</div>
		
		
		
		<form id="edit_profile" name="edit_profile" method="post" class="big_clean" accept-charset="utf-8">
		
			<div class="row grid_17">
				<label for="content"><?php _e( 'Username', ns_text_domaine ); ?></label>
				<input type="text" name="user_login" id="user_login" value="<?php echo $current_user->user_login; ?>" disabled="disabled" class="rounded">
				<p><?php _e( 'Usernames cannot be changed.', ns_text_domaine ); ?></p>
			</div>
		
			<div class="row grid_17">
				<label for="content"><?php _e( 'First Name', ns_text_domaine ); ?></label>
				<input type="text" name="first_name" id="first_name" value="<?php echo $current_user->first_name; ?>" class="rounded">
			</div>
		
			<div class="row grid_17">
				<label for="content"><?php _e( 'Last Name', ns_text_domaine ); ?></label>
				<input type="text" name="last_name" id="last_name" value="<?php echo $current_user->last_name; ?>" class="rounded">
			</div>
		
			<div class="row grid_17">
				<label for="content"><?php _e( 'Display name publicly as', ns_text_domaine ); ?></label>
				
				<select name="display_name" id="display_name" class="rounded">
				<?php
					$public_display = array();
					$public_display['display_nickname']  = $current_user->nickname;
					$public_display['display_username']  = $current_user->user_login;
		
					if ( !empty($current_user->first_name) )
						$public_display['display_firstname'] = $current_user->first_name;
		
					if ( !empty($current_user->last_name) )
						$public_display['display_lastname'] = $current_user->last_name;
		
					if ( !empty($current_user->first_name) && !empty($current_user->last_name) ) {
						$public_display['display_firstlast'] = $current_user->first_name . ' ' . $current_user->last_name;
						$public_display['display_lastfirst'] = $current_user->last_name . ' ' . $current_user->first_name;
					}
		
					if ( !in_array( $current_user->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere
						$public_display = array( 'display_displayname' => $current_user->display_name ) + $public_display;
		
					$public_display = array_map( 'trim', $public_display );
					$public_display = array_unique( $public_display );
		
					foreach ( $public_display as $id => $item ) {
				?>
					<option id="<?php echo $id; ?>"<?php selected( $current_user->display_name, $item ); ?>><?php echo $item; ?></option>
				<?php
					}
				?>
				</select>
			</div>
		
			<div class="row grid_17">
				<label for="content"><?php _e( 'E-mail', ns_text_domaine ); ?></label>
				<input type="text" name="email" id="email" value="<?php echo $current_user->user_email; ?>" class="validate[required] rounded">
			</div>
		
			<div class="row grid_17">
				<label for="content"><?php _e( 'Website', ns_text_domaine ); ?></label>
				<input type="text" name="url" id="url" value="<?php echo $current_user->url; ?>" class="rounded">
			</div>
		
			<div class="row grid_17">
				<label for="content"><?php _e( 'Biographical Info', ns_text_domaine ); ?></label>
				<textarea name="description" id="description" rows="5" cols="30"class="rounded"><?php echo $current_user->description; ?></textarea>
				<p><?php _e( 'Share a little biographical information to fill out your profile. This may be shown publicly.', ns_text_domaine ); ?></p>
			</div>
		
			<div class="row grid_17">
				<label for="pass1"><?php _e( 'New Password', ns_text_domaine ); ?></label>
				<input type="password" name="pass1" id="pass1" size="16" value="" autocomplete="off" class="rounded" />
				<p><?php _e( 'If you would like to change the password type a new one. Otherwise leave this blank.', ns_text_domaine ); ?></p><br>
				<input type="password" name="pass2" id="pass2" size="16" value="" autocomplete="off" class="rounded">
				<p><?php _e( 'Type your new password again.', ns_text_domaine ); ?></p>				
			</div>
			
			<div class="row grid_17">
				<?php wp_nonce_field( 'update_profile_' . $user_ID ); ?>
				<input type="hidden" name="action" value="update_profile" />
				<input type="submit" name="profile_submit" id="profile_submit" class="rounded" value="<?php _e( 'Update profile', ns_text_domaine ); ?>">
			</div>
			
		</form>
	
	</div>
	
	<?php get_template_part( 'sidebar', 'user_cp' ); ?>
	
</div>
<!--End #wrapper_main-->

<?php get_footer(); ?>
